using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Reflection;

namespace Ultimate
{
	/// <summary>
	/// Summary description for FromBinding.
	/// </summary>
	public class AutoBind 
	{
		public static void BindObjectToControls(object obj, Control container) 
		{
			if (obj == null) return;
			Type objType = obj.GetType();
			PropertyInfo[] objPropertiesArray = 
				objType.GetProperties();

			foreach (PropertyInfo objProperty in objPropertiesArray) 
			{

				Control control = container.FindControl(objProperty.Name);
				if (control != null) 
				{
					if (control is ListControl) 
					{
						ListControl listControl = (ListControl) control;
						string propertyValue = objProperty.GetValue(obj, 
							null).ToString();
						ListItem listItem = listControl.Items.FindByValue(propertyValue);
						if (listItem != null) listItem.Selected = true;
					} 
//					else if (control is CheckBox) 
//					{
//						if (objProperty.PropertyType == typeof(bool))
//							((CheckBox) control).Checked = (bool) 
//								objProperty.GetValue(obj, null);
//					} 
//					else if (control is Calendar) 
//					{
//						if (objProperty.PropertyType == typeof(DateTime))
//							((Calendar) control).SelectedDate = (DateTime) 
//								objProperty.GetValue(obj, null);
//					} 
//					else if (control is TextBox) 
//					{
//						((TextBox) control).Text = objProperty.GetValue(obj, 
//							null).ToString();
//					} 
//					else if (control is Literal)
					else
					{
						// get the properties of the control
						//
						Type controlType = control.GetType();
						PropertyInfo[] controlPropertiesArray = 
							controlType.GetProperties();

						bool success = false;
						success = FindAndSetControlProperty(obj, 
							objProperty, control, controlPropertiesArray, 
							"Checked", typeof(bool) );

						if (!success)
							success = FindAndSetControlProperty(obj, 
								objProperty, control, controlPropertiesArray, 
								"SelectedDate", typeof(DateTime) );

						if (!success)
							success = FindAndSetControlProperty(obj, 
								objProperty, control, controlPropertiesArray, 
								"Value", typeof(String) );

						if (!success)
							success = FindAndSetControlProperty(obj, 
								objProperty, control, controlPropertiesArray, 
								"Text", typeof(String) );
						
					}
				}
			}
		}
		
	
		private static bool FindAndSetControlProperty(object obj, 
			PropertyInfo objProperty, Control control, 
			PropertyInfo[] controlPropertiesArray, string propertyName, 
			Type type) 
		{
			// iterate through control properties

			foreach (PropertyInfo controlProperty in 	controlPropertiesArray) 
			{
				// check for matching name and type
				if (controlProperty.Name == propertyName && 
					controlProperty.PropertyType == type) 
				{
					// set the control's property to the 
					// business object property value
					controlProperty.SetValue(control, 
						Convert.ChangeType( 
						objProperty.GetValue(obj, null), type) , null);
					return true;
				}
			}
			return false;
		}

		public static void BindControlsToObject(object obj, Control container) 
		{
			Type objType = obj.GetType();
			PropertyInfo[] objPropertiesArray = objType.GetProperties();

			foreach (PropertyInfo objProperty in objPropertiesArray) 
			{
				string tmp = objProperty.Name;
				Control control = container.FindControl(objProperty.Name);
				if (control != null) 
				{
					if (control is ListControl) 
					{
						ListControl listControl = (ListControl) control;
						if (listControl.SelectedItem != null)
							objProperty.SetValue(obj, 
								Convert.ChangeType(listControl.SelectedItem.Value,
								objProperty.PropertyType), null);

					} 
					else 
					{
						// get the properties of the control
						//
						Type controlType = control.GetType();
						PropertyInfo[] controlPropertiesArray = 
							controlType.GetProperties();

						bool success = false;
						success = FindAndGetControlProperty(obj, 
							objProperty, control, controlPropertiesArray, 
							"Checked", typeof(bool) );

						if (!success)
							success = FindAndGetControlProperty(obj, 
								objProperty, control, controlPropertiesArray, 
								"SelectedDate", typeof(DateTime) );

						if (!success)
							success = FindAndGetControlProperty(obj, 
								objProperty, control, controlPropertiesArray, 
								"Value", typeof(String) );

						if (!success)
							success = FindAndGetControlProperty(obj, 
								objProperty, control, controlPropertiesArray, 
								"Text", typeof(String) );

					}
				}
			}
		}
		private static bool FindAndGetControlProperty(object obj, 
			PropertyInfo objProperty, Control control, PropertyInfo[] 
			controlPropertiesArray, string propertyName, Type type) 
		{
			// iterate through control properties
			foreach (PropertyInfo controlProperty in 
				controlPropertiesArray) 
			{
				string tmp = controlProperty.Name;
				// check for matching name and type
				if (controlProperty.Name == propertyName && 
					controlProperty.PropertyType == type) 
				{
					// set the control's property to the 
					// business object property value
					try 
					{
						objProperty.SetValue(obj, 
							Convert.ChangeType( 
							controlProperty.GetValue(control, null), 
							objProperty.PropertyType) , null);
						return true;
					} 
					catch 
					{
						// the data from the form control 
						// could not be converted to 
						// objProperty.PropertyType
						return false;
					}
				}
			}
			return false;
		}
	}
}
